﻿using System;
using System.Web.UI.WebControls;
using MyHealthByDesign.Domain;
using System.Collections.Generic;

namespace MyHealthByDesign.Admin
{
    /// <summary>
    /// Description:
    /// Displays website category maintenance for the administration section
    /// 
    /// Author: Mark Servais
    /// Date Created: 12/02/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// None. 
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public partial class MaintainCategories : System.Web.UI.Page
    {
        Domain.Domain domain = new Domain.Domain();

        /// <summary>
        /// Page Load event handler - check if admin is logged in
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!domain.IsAdminLoggedIn())
            {
                Response.Redirect("Default.aspx");
            }

            if (!Page.IsPostBack)
            { 
                // load list box
                LoadCategoryList();

                // set button visibility
                ResetButtons();
            }
        }

        /// <summary>
        /// LoadCategoryList - populates categories in list box fo maintenance
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        protected void LoadCategoryList()
        {
            lbCategories.Items.Clear();

            // add first item
            lbCategories.Items.Add(new ListItem("<Add new Category>", "AA"));

            List<QuestionCategory> lstCategories = domain.GetCategories();

            // add database items
            foreach (var questionCategory in lstCategories)
            {
                lbCategories.Items.Add(new ListItem(questionCategory.CategoryDescription, questionCategory.CategoryId.ToString()));
            }

        }

        /// <summary>
        /// ResetButtons - resets maintenance buttons
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        protected void ResetButtons()
        {
            btnUpdate.Visible = false;
            btnDelete.Visible = false;
            btnAdd.Visible = false;
            tbCategoryName.Visible = false;
            lblErrors.Visible = false;
        }

        /// <summary>
        /// lbCategories_SelectedIndexChanged - process selection made
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Appropriate buttons are displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            // determine if addition needed
            if (lbCategories.SelectedValue == "AA")
                RenderAdd();
            else
            {
                RenderUpdateDelete();
            }

            lblErrors.Text = string.Empty;
        }

        /// <summary>
        /// RenderAdd - render the textbox and button for an addition
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Appropriate buttons are displayed
        /// </summary>
        ///
        private void RenderAdd()
        {
            btnUpdate.Visible = false;
            btnDelete.Visible = false;
            btnAdd.Visible = true;
            tbCategoryName.Visible = true;
            tbCategoryName.Text = string.Empty;
        }

        /// <summary>
        /// RenderUpdateDelete - render the textbox and buttons for an update and delete
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Appropriate buttons are displayed
        /// </summary>
        ///
        private void RenderUpdateDelete()
        {
            btnUpdate.Visible = true;
            btnDelete.Visible = true;
            btnAdd.Visible = false;
            tbCategoryName.Visible = true;
            tbCategoryName.Text = lbCategories.SelectedItem.Text;
        }

        /// <summary>
        /// btnAdd_Click - add new category
        /// 
        /// PreConditions:
        /// textbox for category descriptino must be populated
        /// 
        /// PostConditions:
        /// new category will be inserted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (tbCategoryName.Text != string.Empty)
                if (domain.InsertCategory(tbCategoryName.Text))
                {
                    LoadCategoryList();
                    ResetButtons();
                }

        }

        /// <summary>
        /// btnDelete_Click - delete category
        /// 
        /// PreConditions:
        /// list box category must be selected
        /// 
        /// PostConditions:
        /// category will be removed from the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (domain.RetrieveQuestions(Convert.ToInt32(lbCategories.SelectedValue)).Count == 0)
            {
                if (domain.DeleteCategory(Convert.ToInt32(lbCategories.SelectedValue)))
                {
                    LoadCategoryList();
                    ResetButtons();
                }
            }
            else
            {
                lblErrors.Text +=
                    "The current selected category has questions associated with it. Please move questions to another category and retry your delete.";
                lblErrors.Visible = true;
            }
        }

        /// <summary>
        /// btnUpdate_Click - update category description
        /// 
        /// PreConditions:
        /// textbox for category descriptino must be populated and
        /// list box category must be selected
        /// 
        /// PostConditions:
        /// category description will be update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbCategoryName.Text != string.Empty)
                if (domain.UpdateCategory(Convert.ToInt32(lbCategories.SelectedValue), tbCategoryName.Text))
                {
                    LoadCategoryList();
                    ResetButtons();
                }
            
        }
    }
}